package com.ebelter.btlibrary.btble.impl.ichoice;

import android.app.Activity;
import android.bluetooth.BluetoothDevice;

import com.ebelter.btlibrary.btble.ble.BleManager;
import com.ebelter.btlibrary.btble.ble.bluetooth.callback.ConnectCallback;
import com.ebelter.btlibrary.btble.ble.bluetooth.device.BlueToothServer;
import com.ebelter.btlibrary.btble.ble.model.BleConfigure;
import com.ebelter.btlibrary.btble.ble.model.BleDevice;
import com.ebelter.btlibrary.btble.ble.model.BleType;
import com.ebelter.btlibrary.btble.impl.ichoice.callback.IChoiceResultCallback;
import com.ebelter.btlibrary.btble.impl.scale.ScaleCommondEditor;
import com.ebelter.btlibrary.btble.impl.scale.ScaleMessageAnalyser;
import com.ebelter.btlibrary.btble.impl.scale.callback.IMeasureResultCallback;
import com.ebelter.btlibrary.btble.impl.scale.callback.WeightUnitChangedCallback;
import com.ebelter.btlibrary.btble.impl.scale.common.ScaleBleConfigure;
import com.ebelter.btlibrary.btble.impl.scale.model.FileType;
import com.ebelter.btlibrary.btble.impl.scale.model.ScaleUser;
import com.ebelter.btlibrary.btble.impl.scale.upgrade.FirmwareUpgradeCallback;
import com.ebelter.btlibrary.btble.impl.scale.upgrade.FirmwareUpgrader;


/**
 * 血氧仪蓝牙管理者
 * Created by Sergio Pan on 2017/12/8.
 */

public class IChoiceBle extends BleManager {

    private static IChoiceMessageAnalyser analyser;
    private static IChoiceCommondEditor editor;
    private static IChoiceBle instance;

    private IChoiceBle() {
    }

    public static synchronized IChoiceBle getInstance() {
        if (instance == null) {
            instance = new IChoiceBle();
        }
        return instance;
    }

    /**
     * 1. 初始化蓝牙服务
     *
     * @param aty
     */
    @Override
    protected void initBleService(Activity aty) {
        if (aty == null) {
            return;
        }


        //打开蓝牙
        open(aty);
        //创建实例
        analyser = IChoiceMessageAnalyser.getInstance();
        mBlueToothServer = BlueToothServer.getInstance(aty.getApplicationContext());
        mBlueToothServer.registerScanResultCallback(scanResultCallback);
        mBlueToothServer.registerConnectCallback(mConnectCallback);
        mBlueToothServer.registerBleMessageAnalyser(analyser);

        editor = IChoiceCommondEditor.getInstance();
        editor.registerBleMessageWriter(mBlueToothServer);
        analyser.registerInternalChannel(editor);

        //已绑定设备开始扫描
        if (instance.isDeviceBound()) {
            instance.startScan();
        }


        //注册息屏监听广播
        instance.registSreenStatusReceiver();
    }

    @Override
    protected void setBleParams() {
        //设置数据头
        BleConfigure configure = BleDevice.getInstance().getConfigure();
        analyser.setDataHeaderReceive(configure.getDataHaderReceive());
        editor.setDataHeaderSend(configure.getDataHeaderSend());
    }


    public void registerMeasureResultCallback(IChoiceResultCallback callback) {
        analyser.registerMeasureResultCallback(callback);
    }





    private static ConnectCallback mConnectCallback = new ConnectCallback() {
        @Override
        public void onConnected(BluetoothDevice device) {
            //TODO 设置蓝牙设备参数
            mBlueToothServer.stopScanDevice();
            if (customConnectCallback != null) {
                customConnectCallback.onConnected(device);
            }
            editor.sendInit();
            //请求秤版本信息
//            editor.requestScaleVersion();
            instance.mConnectStatus = ConnectStatus.CONNECTED;
        }

        @Override
        public void onScaleWake() {
            if (customConnectCallback != null) {
                customConnectCallback.onScaleWake();
            }
        }

        @Override
        public void onScaleSleep() {
            if (customConnectCallback != null) {
                customConnectCallback.onScaleSleep();
            }
        }

        @Override
        public void onDisConnected() {
            if (customConnectCallback != null) {
                customConnectCallback.onDisConnected();
            }
//            editor.stopConnHeartbeat();
            instance.mConnectStatus = ConnectStatus.DISCONNECTED;
        }
    };


}
